# frozen_string_literal: true

desc 'Run features for all languages'
task :cucumber do
  dir = File.dirname(__FILE__)
  Dir["#{dir}/*"].sort.each do |f|
    next unless File.directory?(f)

    lang = f[dir.length + 1..]
    message = examples_disabled?(lang)
    unless message.nil?
      $stderr.puts(message)
      next
    end

    Dir.chdir(f) do
      puts "DIR: #{f}"
      rake('cucumber')
    end
  end
end

task default: :cucumber

def examples_disabled?(lang)
  return make_warning("SKIPPING #{lang} (The examples are out of date - please help update them)") unless examples_working?(lang)

  make_warning("SKIPPING #{lang}: examples have been disabled for JRuby.") if jruby_disabled_examples?(lang)
end

def jruby_disabled_examples?(lang)
  return unless RUBY_PLATFORM == 'java'

  %w[ru uk uz].include?(lang)
end

def examples_working?(lang)
  !%w[ar].index(lang)
end

def make_warning(msg)
  %(
    !!!!!
    !!!!!
    !!!!! #{msg}
    !!!!!
    !!!!!
  )
end

def rake(args)
  ruby($PROGRAM_NAME, args)
end
